package nahuel.games.carracer;

import java.util.Random;

import nahuel.games.carracer.Juego.Personaje;

public class Ladron extends Auto {
	
	private int velRelativaY = 0;
	private Random rand;
	private long timeLadron = 0;

	private static final int DELAY_LADRON = 3000;

	public Ladron(GameView vista, int anchoD, int altoD) {
		super(vista, Personaje.ladron, anchoD, altoD);
		rand = new Random();
		this.velY = VEL_EXTRA;
		posInicial();
	}

	@Override
	public void posInicial(){
		x = (gameView.getWidth() / 5) * 3;
		y = gameView.getHeight() + height;
	}

	@Override
	public boolean chequeoLimites(){
		if (x < gameView.getWidth() * 8 / 45) {
			x = gameView.getWidth() * 8 / 45;
		}
		if (x > (gameView.getWidth() * 37 / 45) - width) {
			x = (gameView.getWidth() * 37 / 45) - width;
		}
		this.velY = VEL_RAPIDA;
		return true;
	}

	public void update() {
		x += gameView.escalarVelocidadX(velX);
		y -= gameView.escalarVelocidadY(velY + velRelativaY);
		chequeoLimites();
		if(timeLadron + DELAY_LADRON < System.currentTimeMillis()){
			cambiarVelocidad(rand.nextInt(100));
			timeLadron = System.currentTimeMillis();
		}
		velX = VEL_DETENIDO;
	}

	private void cambiarVelocidad(int porc){
		if ((porc > 0) && (porc <= 5)){
			this.velY = VEL_EXTRA;
		}
		if ((porc > 5) && (porc <= 15)){
			this.velX = 0;
			this.velY = VEL_MAXIMA;
		}
		if ((porc > 15) && (porc <= 30)){
			this.velX = 0;
			this.velY = VEL_CRUCERO;
		}
		if (porc > 30){
			this.velX = 0;
			this.velY = VEL_RAPIDA;
		}
	}
	
	public void setVelocidadRelativa(int x, int y) {
		this.velRelativaY = -y;
	}
	
	@Override
	public void choque(Auto otro) {
		this.velY = otro.velY * 2/3;
		this.velX = otro.velX * 3/2;
		otro.velY = otro.velY * 1/3;
		otro.velX = -otro.velX * 3/2;
		vida.bajarVida();
		update();
	}
}
